﻿'**************************************************************
'CATECO_UDF_001.0  catia / ecotect interoperability
'ben howes, matt naugle, alex gonzales - 05.09.11
'
'This work is licensed under the GNU Generic Public License v3.
'See http://www.gnu.org/licenses/gpl.html For more license information.
'
'
'Creates a link between catia And ecotect Using catia's exposed COM objects 
'And ecotect's dde server.  Props to the proving ground (and uto) for the dde trick.
'
'
'**************************************************************

'import NDde namespace for DDE protocol with ecotect
'make sure to browse for and add the ndde.dll
Imports NDde


Public Class Cateco
	
	'private variables
	Private myWeatherFile As String
	Private myShadingFile As String
	Private myObjectList As New ArrayList()
	Private myAttributeList As New ArrayList()
	Private myScaleMin As Double
	Private myScaleMax As Double
	Private myCalcDates As String
	Private myNumShadingObjects As Integer = 0
	
	private cmd As ndde.Client.DdeClient
	private rq As ndde.Client.DdeClient
	
	
	
	'properties
	Property weatherFile() As String
		Get
			return myWeatherFile
		End Get
		Set(ByVal weather as String)
			myWeatherFile = weather
		End Set
	End Property
	
	Property shadingFile() As String
		Get
			Return myShadingFile
		End Get
		Set(ByVal shading As String)
			myShadingFile = shading
		End Set
	End Property
	
	ReadOnly Property objectList As ArrayList
		Get
			return myObjectList
		End Get
	End Property
	
	ReadOnly Property attributeList As ArrayList
		Get
			return myAttributeList
		End Get
	End Property
	
	ReadOnly Property scaleMin as double
		Get
			return myScaleMin
		End Get
	End Property
	
	ReadOnly Property scaleMax as Double
		Get
			return myScaleMax
		End Get
	End Property
	
	ReadOnly Property calcDates As String
		Get
			Return myCalcDates
		End Get
	End Property
	
	ReadOnly Property numShadingObjects As Integer
		Get
			Return myNumShadingObjects
		End Get
		
	End Property
	
	
	
	'public methods
	
	''INITIALIZATION STUFF
	''connect to ecotect
	Public Sub connect()	
		cmd = New ndde.Client.DdeClient("Ecotect", "execute")
		cmd.Connect()
		rq = New ndde.Client.DdeClient("Ecotect", "request")
		rq.Connect()	
	End Sub
	
	''disconnect from ecotect
	Public Sub disconnect()
		cmd.Disconnect()
		rq.Disconnect()
	End Sub
	
	''activate ecotect
	Public Sub activate()
		Dim appActivate As String = "app.activate"
		cmd.Execute(appActivate, 500)
	End Sub
	
	''create a new model
	Public Sub newModel()
		Dim newEcoModel As String = "model.new"
		cmd.Execute(newEcoModel, 5000)
	End Sub
	
	''update model
	Public Sub update()
		Dim updateModel As String = "model.update"
		cmd.Execute(updateModel, 500)
	End Sub
	
	

	''load weather file
	Public Sub loadWeatherFile()
		Dim loadMyWeatherFile As String = "weather.load.all, " + myWeatherFile
		cmd.Execute(loadMyWeatherFile, 6000)
	End Sub
	
	''load shading file
	Public Sub loadShadingFile()
		Dim loadMyShadingFile As String = "model.load " + myShadingFile
		cmd.Execute(loadMyShadingFile, 1000)
		
		Dim getModelObjects As String = "get.model.objects"
		myNumShadingObjects = rq.Request(getModelObjects, 1000)
	End Sub
	
	''add a new zone for the analysis objects, named CATECO
	Public Sub addZone()
		Dim addMyZone As String = "add.zone CATECO"
		rq.Request(addMyZone, 500)
		
		Dim myCurrentZone As String = "set.zone.current 1"
	End Sub
	
	
	
	
	''CREATE OBJECTS
	''create a wall object
	Public Sub createWallObject()
		Dim createMyWallObject As String = "add.object 4 12"
		myObjectList.Add(rq.Request(createMyWallObject, 5000))
	End Sub
	
	''add nodes to object
	Public Sub addNodeToObject(ByVal objectIndex As Integer, byVal nodeIndex as Integer, ByVal x As Double, ByVal y As Double, ByVal z As Double)
		dim addMyNodeToObject as String = "add.node " + objectIndex.ToString + " " + nodeIndex.ToString + " " + x.ToString + " " + y.ToString + " " + z.ToString
		rq.Request(addMyNodeToObject, 5000)
	End Sub
	
	''close out an object
	Public Sub objectDone()
		Dim myObjectDone As String = "object.done"
		cmd.Execute(myObjectDone, 5000)
	End Sub
	
	''fit grid and view to all objects in the model
	Public Sub fitGrid()
		Dim fitMyView As String = "view.fitgrid"
		cmd.Execute(fitMyView, 5000)
	End Sub
	
	
	''INSOLATION STUFF
	''set date range for analysis
'	Public Sub setCalcDates(byval startJulianDate as Integer, byval endJulianDate as Integer, byval includeStartAndStop as integer)
'		Dim setMyCalcDates As String = "set.calc.dates " + startJulianDate.ToString + " " + endJulianDate.ToString + " " + includeStartAndStop.ToString
'		rq.Request(setMyCalcDates, 500)
'	End Sub
	
	''set date range for analysis
	Public Sub setCalcDates(byval startJulianDate as Integer, byval endJulianDate as Integer)
		Dim setMyCalcDates As String = "set.calc.dates " + startJulianDate.ToString + " " + endJulianDate.ToString
		cmd.Execute(setMyCalcDates, 500)
	End Sub
	
	''get calc dates... good for debugging
	Public Sub getCalcDates()
		Dim getMyCalcDates As String = "get.calc.dates"
		myCalcDates = rq.Request(getMyCalcDates, 500)
	End Sub
	
	''set time range for analysis
	Public Sub setClacTimes(ByVal startTime As Double, ByVal endTime As Double, ByVal includeStartAndStop As Integer)
		Dim setMyCalcTimes As String = "set.calc.times " + startTime.ToString + " " + endTime.ToString  + " " + includeStartAndStop.ToString
		cmd.Execute(setMyCalcTimes, 500)
	End Sub
	
	''set calculation precision
	Public Sub setCalcPrecision(ByVal p As Integer)
		Dim setMyCalcPrecision As String = "set.calc.precision " + p.ToString
		cmd.Execute(setMyCalcPrecision, 500)
	End Sub
	
	''run insolation calculations on objects
	Public Sub calcInsolation()
		Dim myCalcInsolation As String = "calc.insolation objects 0 0 0"
		cmd.Execute(myCalcInsolation, 1000000)
	End Sub
	
	''run insolation based on user input
	Public Sub calcInsolation(ByVal AnalysisType As Integer, ByVal Select3D As String, ByVal Accumulation As Integer)
		Dim myCalcInsolation As String = "calc.insolation objects " + AnalysisType.ToString + " " + Select3D + " " + Accumulation.ToString
		cmd.Execute(myCalcInsolation, 1000000)
	End Sub
	
	''request insolation results for an object
	Public Sub requestInsolationValue(ByVal attributeNumber As Integer, ByVal objectIndex As Integer)
		Dim requestMyInsolationValue As String = "get.object.attr" + attributeNumber.ToString + " " + objectIndex.ToString
		dim myDouble as String = rq.Request(requestMyInsolationValue, 1000)
		myAttributeList.Add(myDouble)
	End Sub
	
	''get the max and min values of the analysis scale
	Public Sub getAttributeScale()
		Dim getMyAttributeScale As String = "get.attribute.scale"
		Dim myValues As String = rq.Request(getMyAttributeScale, 1000)
		Dim myValuesArray() As String = split(myValues, ",")
		myScaleMin = CDbl(myValuesArray(0))
		myScaleMax = CDbl(myValuesArray(1))
	End Sub
	
	
	
	'public constructors
	
	
End Class

